package com.demo.eshop.pdetail.thread;

import com.demo.eshop.pdetail.request.Request;
import com.demo.eshop.pdetail.request.RequestQueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: e20160504
 * @date: 2018/10/29
 * @desc
 */
public class RequestProcesserThreadPool {


    //线程池
    private ExecutorService threadPool = Executors.newFixedThreadPool(10);



    public RequestProcesserThreadPool(){

        //初始化内存队列和线程池
        RequestQueue requestQueue = RequestQueue.getInstance();
        for(int i=0;i<10;i++){
            ArrayBlockingQueue<Request> queue = new ArrayBlockingQueue<Request>(10);
            requestQueue.addQueue(queue);
            threadPool.submit(new RequestProcesserThread(queue));
        }
    }

    /**
     * @params:
     * @return:
     * @desc: 单例：此处取用绝对线程安全的方式实现。静态内部类
     */
    public static class SingleTon {

        private static RequestProcesserThreadPool instance;

        static {
            instance = new RequestProcesserThreadPool();
        }

        private static RequestProcesserThreadPool getInstance(){
            return instance;
        }
    }

    public static RequestProcesserThreadPool getInstance(){
        return SingleTon.getInstance();
    }

    public static RequestProcesserThreadPool init(){
        return getInstance();
    }
}
